package com.thinkgem.jeesite.modules.inventory.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.monitor.zabbix.service.ZabbixApiService;
import com.thinkgem.jeesite.common.web.BaseController;


/**
* 项目名称：zoomSite   
* 类名称：ZlmtHostStatusQueryController   
* 类描述：  机房监控 全部机器状态查询控制器
* 创建人：XuanYC   
* 创建时间：2016年7月28日 上午11:53:02   
* 修改人：XuanYC   
* 修改时间：2016年7月28日 上午11:53:02   
* 修改备注：   
* @version
 */
@Controller
@RequestMapping(value = "${adminPath}/zlmtHost/statusQuery")
public class ZlmtHostStatusQueryController extends BaseController{
	private static final Logger logger = LoggerFactory.getLogger(ZlmtHostStatusQueryController.class);

	@Resource
	private ZabbixApiService zabbixApiService;
	
	
	/**
	 * 查询所有机器的当前状态信息
	 * @param httpServletRequest
	 * @param httpServletResponse
	 * @return
	 */
	@SuppressWarnings({ "unused", "unchecked" })
	@RequestMapping(value = "queryAllStatus" , method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> queryAllStatus(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse){
		Map<String, Object> model = new HashMap<String, Object>();
		try {
			Map<String, Object> newHost = new HashMap<String, Object>();;
			//查询所有机器，传入参数为空即可
			Map<String, Object> nullMap = new HashMap<String, Object>();
			Map<String, Object> host = zabbixApiService.getHost(nullMap);
			
			List<Map<String, Object>>  problems = zabbixApiService.findProblem(nullMap);
			List<String> ids = new ArrayList<>();
			for(Map<String, Object> thisMap : problems){
				ids.add(thisMap.get("triggerid").toString());
			}
			
			Map<String, Object> params = new HashMap<>();
			params.put("ids", ids);
			List<Map<String, Object>>  hostList = zabbixApiService.findHostByTrigger(params);
			
			Map<String,Map<String,Object>> hostMap = new HashMap<>();
			for(Map<String, Object> thisMap : hostList){
				hostMap.put(thisMap.get("hostid").toString(), thisMap);
			}
			
			if (host.containsKey("result")) {
				List<JSONObject> list = (List<JSONObject>) host.get("result");
				for (JSONObject json : list) {
					if("2".equals( json.get("available"))){
						continue;
					}
					String hostid = json.get("hostid").toString();
					if(hostMap.containsKey(hostid)){
						json.put("war", "1");
					}else{
						json.put("war", "0");
					}
				}
				newHost.put("result", list);
			}
			
			//从新的host获取机器状态转换信息
			if(newHost != null && newHost.size() > 0){
				Object obj = newHost.get("result");
				if(obj != null){
					List <Map<String , String>> result = (List <Map<String , String>>)obj;
					if(result != null && result.size() > 0){
						for(Map<String , String> map : result){
							String hostName = map.get("host");
							String available = map.get("available");
							String war = map.get("war");
							String hostStatus = "start";
							//正常机器
							if("1".equals(available) &&  "0".equals(war)){
								hostStatus = "on";
							}
							//异常机器
							if("1".equals(available) &&  "1".equals(war)){
								hostStatus = "war";
							}
							//宕机
							if("2".equals(available)){
								hostStatus = "down";
							}
							model.put(hostName.replaceAll(" ", ""), hostStatus);
						}
					}
				}
			}
			return model;
		}catch (Exception e) {
			logger.error("--queryAllStatus 查询机房中服务器状态出现异常，异常信息：" + e.getMessage());
			return null;
		}
	}


}
